//
//  BattleLayer.m
//  VicBattle
//
//  Created by Nguyen Nguyen on 5/3/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "VBBattleLayer.h"
#import "VBCharacter.h"
#import "VBArcher.h"
#import "VBKnight.h"
#import "VBNinja.h"
#import "VBGunner.h"
#import "VBHealer.h"
#import "VBSoldier.h"
#import "VBSorcerer.h"
#import "VBDatabase.h"

@implementation VBBattleLayer

@synthesize m, n, w, h;
@synthesize vbChar;
VBMapInfo *map;

-(void) initAnimation
{     
    id pvrTexture;
    
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        pvrTexture = [[CCTextureCache sharedTextureCache] addImage:@"characters01-hd.pvr.ccz"];
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"characters01-hd.plist" texture:pvrTexture];
        pvrTexture = [[CCTextureCache sharedTextureCache] addImage:@"characters02-hd.pvr.ccz"];
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"characters02-hd.plist" texture:pvrTexture];
        pvrTexture = [[CCTextureCache sharedTextureCache] addImage:@"night_claw-hd.pvr.ccz"];
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"night_claw-hd.plist" texture:pvrTexture];
    }
    else
    {
        pvrTexture = [[CCTextureCache sharedTextureCache] addImage:@"characters01.pvr.ccz"];
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"characters01.plist" texture:pvrTexture];
        pvrTexture = [[CCTextureCache sharedTextureCache] addImage:@"characters02.pvr.ccz"];
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"characters02.plist" texture:pvrTexture];
        pvrTexture = [[CCTextureCache sharedTextureCache] addImage:@"night_claw.pvr.ccz"];
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"night_claw.plist" texture:pvrTexture];
    }
    
    CCAnimation *archerSouthIdle = [CCAnimation animation];
    CCAnimation *archerNorthIdle = [CCAnimation animation];
    CCAnimation *knightSouthIdle = [CCAnimation animation];
    CCAnimation *knightNorthIdle = [CCAnimation animation];
    CCAnimation *ninjaSouthIdle = [CCAnimation animation];
    CCAnimation *ninjaNorthIdle = [CCAnimation animation];
    CCAnimation *gunnerSouthIdle = [CCAnimation animation];
    CCAnimation *gunnerNorthIdle = [CCAnimation animation];
    CCAnimation *healerSouthIdle = [CCAnimation animation];
    CCAnimation *healerNorthIdle = [CCAnimation animation];
    CCAnimation *soldierSouthIdle = [CCAnimation animation];
    CCAnimation *soldierNorthIdle = [CCAnimation animation];
    CCAnimation *sorcererSouthIdle = [CCAnimation animation];
    CCAnimation *sorcererNorthIdle = [CCAnimation animation];
    
    for(int i = 1; i < 9; ++i)
    {
        
        [archerSouthIdle addFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] 
                                  spriteFrameByName:[NSString stringWithFormat:@"archer_idle_%.2i.png",i]]];
        
        [archerNorthIdle addFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] 
                                  spriteFrameByName:[NSString stringWithFormat:@"archer_idle_%.2i.png",i+8]]];
        
        [knightSouthIdle addFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] 
                                  spriteFrameByName:[NSString stringWithFormat:@"knight_idle_%.2i.png",i]]];
        [knightNorthIdle addFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] 
                                  spriteFrameByName:[NSString stringWithFormat:@"knight_idle_%.2i.png",i+8]]];

        [ninjaSouthIdle addFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] 
                                 spriteFrameByName:[NSString stringWithFormat:@"ninja_idle_%.2i.png",i]]];
        [ninjaNorthIdle addFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] 
                                 spriteFrameByName:[NSString stringWithFormat:@"ninja_idle_%.2i.png",i+8]]];

        [gunnerSouthIdle addFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] 
                                  spriteFrameByName:[NSString stringWithFormat:@"gunner_idle_%.2i.png",i]]];
        [gunnerNorthIdle addFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] 
                                  spriteFrameByName:[NSString stringWithFormat:@"gunner_idle_%.2i.png",i+8]]];

        [healerSouthIdle addFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] 
                                  spriteFrameByName:[NSString stringWithFormat:@"healer_idle_%.2i.png",i]]];
        [healerNorthIdle addFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] 
                                  spriteFrameByName:[NSString stringWithFormat:@"healer_idle_%.2i.png",i+8]]];

        [soldierSouthIdle addFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] 
                                   spriteFrameByName:[NSString stringWithFormat:@"soldier_idle_%.2i.png",i]]];
        [soldierNorthIdle addFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] 
                                   spriteFrameByName:[NSString stringWithFormat:@"soldier_idle_%.2i.png",i+8]]];

        [sorcererSouthIdle addFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] 
                                    spriteFrameByName:[NSString stringWithFormat:@"sorcerer_idle_%.2i.png",i]]];
        [sorcererNorthIdle addFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] 
                                    spriteFrameByName:[NSString stringWithFormat:@"sorcerer_idle_%.2i.png",i+8]]];

    }

    [[CCAnimationCache sharedAnimationCache] addAnimation:archerSouthIdle name:@"archerSouthIdle"];
    [[CCAnimationCache sharedAnimationCache] addAnimation:archerNorthIdle name:@"archerNorthIdle"];
    [[CCAnimationCache sharedAnimationCache] addAnimation:knightSouthIdle name:@"knightSouthIdle"];
    [[CCAnimationCache sharedAnimationCache] addAnimation:knightNorthIdle name:@"knightNorthIdle"];
    [[CCAnimationCache sharedAnimationCache] addAnimation:ninjaSouthIdle name:@"ninjaSouthIdle"];
    [[CCAnimationCache sharedAnimationCache] addAnimation:ninjaNorthIdle name:@"ninjaNorthIdle"];
    [[CCAnimationCache sharedAnimationCache] addAnimation:gunnerSouthIdle name:@"gunnerSouthIdle"];
    [[CCAnimationCache sharedAnimationCache] addAnimation:gunnerNorthIdle name:@"gunnerNorthIdle"];
    [[CCAnimationCache sharedAnimationCache] addAnimation:healerSouthIdle name:@"healerSouthIdle"];
    [[CCAnimationCache sharedAnimationCache] addAnimation:healerNorthIdle name:@"healerNorthIdle"];
    [[CCAnimationCache sharedAnimationCache] addAnimation:soldierSouthIdle name:@"soldierSouthIdle"];
    [[CCAnimationCache sharedAnimationCache] addAnimation:soldierNorthIdle name:@"soldierNorthIdle"];
    [[CCAnimationCache sharedAnimationCache] addAnimation:sorcererSouthIdle name:@"sorcererSouthIdle"];
    [[CCAnimationCache sharedAnimationCache] addAnimation:sorcererNorthIdle name:@"sorcererNorthIdle"];
    
    //init night claw (ninja's skill)
    
    CCAnimation *nightclawEffect = [CCAnimation animation];
    
    for(int i = 0; i < 6; ++i)
    {
        [nightclawEffect addFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] 
                                     spriteFrameByName:[NSString stringWithFormat:@"night_claw_effect_%i.png",i]]];
    }
    
    [[CCAnimationCache sharedAnimationCache] addAnimation:nightclawEffect name:@"nightClawEffect"];
    
    CCAnimation *nightclawHit = [CCAnimation animation];
    for(int i = 0; i < 9; ++i)
    {
        [nightclawHit addFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] 
                                   spriteFrameByName:[NSString stringWithFormat:@"night_claw_hit.0_%i.png",i]]];
    }
    
    [[CCAnimationCache sharedAnimationCache] addAnimation:nightclawHit name:@"nightClawHit"];
}

-(id) init
{
    if((self=[super init]))
    {
        CGSize winSize = [[CCDirector sharedDirector] winSize];
        
        [self.camera setCenterX:0 centerY:0 centerZ:-1];
        [self.camera setEyeX:0 eyeY:0 eyeZ:0];
        
        NSString *bgName;
        
        if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        {
            x0 = 242;
            y0 = 338;
            w = 45;
            h = 22.5;
            bgName = @"map02-hd.png";
        }
        else
        {
            x0 = 58;
            y0 = 128;
            w = 30;
            h = 15;
            bgName = @"map02.png";
        }
        
        m = 5;
        n = 9;
        xa = x0-w;
        ya = y0;
        xb = xa + m*w;
        yb = ya - m*h;
        
        CCSprite *bg = [CCSprite spriteWithFile:bgName];
        bg.anchorPoint = ccp(0,0);
        
        outWidth = bg.textureRect.size.width - winSize.width;
        outHeight = bg.textureRect.size.height - winSize.height;
        
        [self addChild:bg];
        [self initAnimation];
        
        //call test database
        VBOffsetInfo *offset = [[VBDatabase database] getOffsetInfoById:1];
        CCLOG(@"%@, %f, %f", offset->name, offset->x, offset->y);        
        
        VBMapInfo *map = [[VBDatabase database] getMapById:1];
        for(int i = 0; i<= 12 ; i++)
            for(int j = 0; j<=12 ; j++)
            {
                if(map->data[i][j] != 'E' && map->data[i][j] != 'N' &&
                   map->data[i][j] != '1' && map->data[i][j] != '2' &&
                   map->data[i][j] != '3' && map->data[i][j] != '4' &&
                   map->data[i][j] != '5' && map->data[i][j] != '6' &&
                   map->data[i][j] != '7' && map->data[i][j] != '8' && 
                   map->data[i][j] != '9')
                    map->data[i][j] ='0';
                CCLOG(@"data o vi tri %i, %i la %c", i, j, map->data[i][j]);
            }
        CCLOG(@"%@, %i, %i, %f, %f", map->name, map->col, map->row, map->x0, map->y0);
    }
    return self;
}


-(CGPoint) pointFromRow:(int) row andCol:(int) col
{
    return ccp(x0 + w*(col+row), y0 + h*(col-row));
}

-(void) getRow:(int *) row andCol:(int *) col fromPoint:(CGPoint) p
{
    float x, y, z;
    [self.camera centerX:&x centerY:&y centerZ:&z];
    
    xp = p.x + x;
    yp = p.y + y;
    
    float pm = fabsf(xp - xa);
    float mr = pm * 0.5;
    yr = yp - mr;
    
    float pn = fabsf(yp - yb);
    float nc = pn * 2;
    xc = xp + nc;
    
    *row = -1, *col = -1;
    
    if(ya >= yr && yr >= ya - 2*m*h && xb <= xc && xc <= xb + 2*n*w)
    {
        *row = (int)(ya-yr)/(2*h);
        *col = (int)(xc-xb)/(2*w);
    }
}

-(void) putCharacter:(int) idNumber toRow:(int) row toCol:(int) col
{
    
    switch (idNumber) {
        case 0:
            vbChar = [[VBArcher alloc] initWithWorld:self];
            break;
        case 1:
            vbChar = [[VBKnight alloc] initWithWorld:self];
            break;
        case 2:
            vbChar = [[VBNinja alloc] initWithWorld:self];
            break;
        case 3:
            vbChar = [[VBGunner alloc] initWithWorld:self];
            break;
        case 4:
            vbChar = [[VBHealer alloc] initWithWorld:self];
            break;
        case 5:
            vbChar = [[VBSoldier alloc] initWithWorld:self];
            break;
        case 6:
            vbChar = [[VBSorcerer alloc] initWithWorld:self];
            break;
            
        default:
            break;
    }
    if(vbChar.body != nil)
    {
        CGPoint p = [self pointFromRow:row andCol:col];
        p.y = p.y + (h/3)*2;
        vbChar.body.position = p;
        [vbChar setCellWithRow:row andCol:col];
        [heroes addObject:vbChar];
    }
}

-(void) viewWithOlPoint:(CGPoint) oldPoint andNewPoint:(CGPoint) newPoint
{
    float x, y, z;
    [self.camera centerX:&x centerY:&y centerZ:&z];
    
    float offsetX = newPoint.x - oldPoint.x;
    float offsetY = newPoint.y - oldPoint.y;
    
    x -= offsetX;
    y -= offsetY;
    
    if(x < 0)
        x = 0;
    else if(x > outWidth)
        x = outWidth;
    if(y < 0)
        y = 0;
    else if(y > outHeight)
        y = outHeight;
    
    [self.camera setCenterX:x centerY:y centerZ:z];
    [self.camera setEyeX:x eyeY:y eyeZ:0];
}

-(void) dealloc
{
    for (id hero in heroes) {
        [hero release];
    }
    [heroes release];
	[[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
	[super dealloc];
}

@end
